package com.mutouren.web.org;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.modules.org.model.Organization;
import com.mutouren.modules.org.service.OrganizationService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/org/organization")
public class OrganizationAction {
	
	@Resource
	private OrganizationService organizationService;
	
	@RequestMapping(value="/manage.action")
	public String manage(
			@RequestParam(value="parentId", required=false, defaultValue="0") int parentId,
			HttpServletRequest request) {
		
		// 父组织
		Organization parent = organizationService.get(parentId);
		request.setAttribute("parentOrgId", parent.getOrgId());
		request.setAttribute("parentOrgType", parent.getOrgType());
		
		// 面包屑
		List<Organization> parentOrgPath = organizationService.getParents(parent, true);	
		if (parentId == Organization.ROOT_ID) {
			parentOrgPath = new ArrayList<Organization>();
		}
		request.setAttribute("parentOrgPath", parentOrgPath);		
		
		// 子组织
		List<Organization> listOrganization = organizationService.selectByParentId(parentId);
		request.setAttribute("listOrganization", listOrganization);
		
		return "modules/org/organizationList";
	}
	
	@RequestMapping(value="/add.action")
	public String add(@ModelAttribute("organization") Organization organization, 
			//@RequestParam(value="orgType") String orgType,
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			Organization parent = organizationService.get(organization.getParentId());
			organization.setParentId(parent.getOrgId());
			organization.setChineseParentPath(parent.getChineseAllPath());
			//organization.setOrgType(orgType);
			return "modules/org/organizationForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, organization, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		organizationService.add(organization);
		return WebUtils.successDialog(response);
	}
	
	@RequestMapping(value="/modify.action")
	public String modify(@ModelAttribute("organization") Organization organization,
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			organization = organizationService.get(organization.getOrgId());
			map.addAttribute("organization", organization);
			return "modules/org/organizationForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, organization, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}		
		
		int result = organizationService.modify(organization);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}
	}
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="orgId") int orgId, String validState,
			HttpServletResponse response) throws IOException {
		Organization organization = new Organization();
		organization.setOrgId(orgId);
		organization.setValidState(ValidState.valueOf(validState));
		
		if (organizationService.setState(organization) >= 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}
	
	@RequestMapping(value={"/treeView.action"})
	public String treeView(
			@RequestParam(value="orgId", required=false, defaultValue="0") Integer orgId,
			@RequestParam(value="isContainSelf", required=false) String isContainSelf,
			@RequestParam(value="displayLevel", required=false, defaultValue="Normal") String displayLevel,
			HttpServletRequest request) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		isContainSelf = isContainSelf == null ? "false" : isContainSelf;
		ValidState vsDisplayLevel = ValidState.valueOf(displayLevel);
		
		List<Organization> listOrganization = organizationService.getChildren(orgId, true, isContainSelf.equals("true"), false, vsDisplayLevel);
		
		request.setAttribute("listOrganization", OrgUtils.convertOrganizationToJSON(listOrganization));
		return "modules/org/organizationTree";
	}

	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// organization
		if (actionName.equals("add") || actionName.equals("modify")) {
			Organization organization = (Organization) model;
			if (StringUtils.isBlank(organization.getOrgName())) {
				errors.add("名称: 不能为空");
			}
			
			if (organization.getSequence() == null) {
				String sequence = request.getParameter("sequence");
				if (StringUtils.isBlank(sequence)) {
					organization.setSequence(0);
				} else {
					errors.add("顺序: 格式有误，必须为数字");
				}
			}		
		}
		return errors.size() > 0;
	}	

}
